<template>
  <div class="dialog">
    <el-dialog
      :title="'备件'+titleText"
      :visible.sync="showDialog"
      width="35%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-form-item label="备件编号：" class="w100p" prop="sparePartsCode">
          <el-input
            v-model="formData.sparePartsCode"
            clearable
            placeholder="请输入备件编号"
          />
        </el-form-item>

        <el-form-item label="备件名称：" class="w100p" prop="sparePartsName">
          <el-input
            v-model="formData.sparePartsName"
            clearable
            placeholder="请输入备件名称"
          />
        </el-form-item>
        <el-form-item label="单位：" class="w100p" prop="unitName">
          <el-input
            v-model="formData.unitName"
            clearable
            placeholder="请输入单位"
          />
        </el-form-item>
        <el-form-item label="参考价格：" class="w100p" prop="price">
          <el-input
            v-model.trim="formData.price" type="number"
            clearable
            placeholder="请输入参考价格"
            @input="($event) => {
            if($event.startsWith('0') && !$event.includes('.') && $event != 0){
              $set(formData, 'price', $event.replace(/\b(0+)/gi,''))
            }else{
              $set(formData, 'price', Math.abs($event))
            }}"
          >
          <template slot="append">元</template>
        </el-input>
        </el-form-item>
        <el-form-item label="备注：" class="w100p" prop="remark">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            v-model="formData.remark"
            clearable
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Api from "@/api/basic-info-manage/spare-parts-manage";

export default {
  name: '',
  components: { },
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    },
    unitArr: {
      type: [Array],
      default: () => {
        return []
      }
    },
    equipModelList: {
      type: [Array],
      default: () => {
        return []
      }
    }, sparePartTypeArr: {
      type: [Array],
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      formData: {},
      rules: {
        sparePartsCode: [
          { required: true, message: "请输入备件编号", trigger: ["change", "blur"] }
        ],
        sparePartsName: [
          { required: true, message: "请输入备件名称", trigger: ["change", "blur"] }
        ],
        // price: [
        //   { required: true, message: "请输入价格", trigger: ["change", "blur"] }
        // ],
        unitName: [
          { required: true, message: "请输入单位", trigger: ["change", "blur"] }
        ]
      },
      showDialog: this.show
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
      this.$nextTick(() => {
        this.$refs['ruleForm']?.clearValidate();
      });
        this.formData = {}

      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            Api.edit(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.msg);
              }
            });
          } else if (this.nowChanceStau === "add") {
            this.formData.organizationNo = sessionStorage.organizationNo
            Api.add(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg);
                this.close()
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {

      this.$nextTick(() => {
        this.$refs['ruleForm']?.clearValidate();
      });
      this.$emit("closeDialog");
    },
    cancel() {
      this.$nextTick(() => {
        this.$refs['ruleForm']?.clearValidate();
      });
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
